import { CallStatus } from "@/types/call";
import { FriendStatus } from "@/types/friend.ts";
import { IMessage, MessageType, ChatType } from "@/types/chat";
import { useRoute } from "vue-router";
import { useChat } from "@/store/modules/chat";
import { useSession } from "@/store/modules/session";
import { useCache } from "@/store/modules/cache";
import { useMitt } from "@/hooks/useMitt";

export function InitHub() {
  const chat = useChat();
  const cache = useCache();
  const session = useSession();
  const route = useRoute();

  // 别人发送给我们的消息
  window.electronApi.getMessage(async (message: IMessage) => {
    console.log("接受到了群消息", message);
    switch (message.chatType) {
      case ChatType.friend:
        await cache.FindFriendListCache(message);
        handleChatMessage(message);
        break;
      case ChatType.group:
        await cache.FindGroupListCache(message);
        handleChatMessage(message);
        break;

      case ChatType.friendNotify:
        await handleFriendNotify(message);
        break;

      case ChatType.heartBeat:
        console.log("接受到了心跳信息");
        break;
    }
  });

  // electron返回我们发送的消息 更新本地
  window.electronApi.updateLocalMessage(async (message: IMessage) => {
    session.UpdateSession(message, true);
    switch (message.messageType) {
      // 需要上传类的文件 我们之前已经手动添加到本地 不需要返回
      case MessageType.file:
      case MessageType.image:
      case MessageType.mixTxtImg:
        break;
      default:
        await chat.UpdateMessage(message);
    }
  });

  // 处理聊天消息（群聊/私聊）
  function handleChatMessage(message: IMessage) {
    if (route.path != "/message") session.GlobalUnreadMessage += 1;
    switch (message.messageType) {
      case MessageType.videoCall:
      case MessageType.voiceCall:
        handleWebRtcMessage(message);
        break;
      default:
        session.UpdateSession(message);
        chat.UpdateMessage(message);
    }
  }

  // 处理视频和语音通话消息
  function handleWebRtcMessage(message: IMessage) {
    switch (message.callStatus) {
      case CallStatus.wait: {
        const { name = "", avatar = "" } = cache.GetFriendInfo(message.sendId)!;
        openNotify(name, avatar, message);
        break;
      }
      // 只有·record记录才会被保存
      case CallStatus.record:
        session.UpdateSession(message);
        chat.UpdateMessage(message);
        break;
      default:
        useMitt.emit("webrtc", message);
        break;
    }
  }

  // 处理好友通知
  async function handleFriendNotify(message: IMessage) {
    switch (message.messageType) {
      case MessageType.requestAdd:
        session.UpdateSession(message);
        break;

      case MessageType.friendOnline:
        cache.UpdateFriendStatus(message.sendId, FriendStatus.online);
        break;

      case MessageType.friendOff:
        cache.UpdateFriendStatus(message.sendId, FriendStatus.off);
        break;
    }
  }

  // 打开通知页面
  function openNotify(name: string, avatar: string, message: IMessage) {
    const { width, height } = window.electronApi.getScreenSize();
    const { chatType, messageType, sendId, sessionId } = message;
    window.electronApi.createWindow({
      title: "notify",
      path:
        "notify?requestParams=" +
        encodeURIComponent(
          JSON.stringify({
            id: sendId,
            chatType,
            messageType,
            sessionId,
            name,
            avatar,
            initiatorId: sendId,
          }),
        ),
      width: 300,
      height: 140,
      x: width - 310,
      y: height - 200,
    });
  }
}
